
// 产品模块
const sequelize = require('../config/operateDB');
const {Sequelize, DataTypes, Op} = require('sequelize');
const {User} = require('./users');
const session = require('koa-session');


const Forums = sequelize.define('forums', {
    id: {
        type: DataTypes.UUID,
        allowNull: false,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
    },
    title: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    content: {
        type: DataTypes.TEXT,
        allowNull: false,
    },
    // 0: 
    type: {
        type: DataTypes.INTEGER,
        allowNull: false,
    },
    commentNumber: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
    },
    userName: {
        type: DataTypes.TEXT,
        allowNull: false,
    }

});

const ForumsComment = sequelize.define('forumsComments', {
    id: {
        type: DataTypes.UUID,
        allowNull: false,
        primaryKey: true,
        defaultValue: Sequelize.UUIDV4,
    },
    comment: {
        type: DataTypes.STRING,
        allowNull: false,
    },
    forumId: {
        type: DataTypes.UUID,
        allowNull: false,
    },
    userName: {
        type: DataTypes.STRING,
        allowNull: false,
    }
});

async function initForums() {
    await Forums.sync();
    await ForumsComment.sync();
}
initForums();

const ForumsModel = {
    async getForums(ctx) {
        const params = ctx.query;
        // const count = await Forums.count();
        const {offset = 0, limit = 10, keywords = '', type = 0} = params;

        let info = {};

        info = await Forums.findAndCountAll(
            {
                where: {
                    title: {
                        [Op.like]: '%' + (keywords || '') + '%',
                    },
                    type: type,
                },
                limit: parseInt(limit),
                offset: parseInt(offset),
                raw: true,
            }
        );
        
        ctx.pageRes({
            limit,
            offset,
            list: info.rows,
            total: info.count,
        });
    },
    async comment(ctx) {
        const params = ctx.request.body;
        if (!params.comment) {
            ctx.errorParams('请输入评论内容！');
            return ;
        } else if (!params.forumId) {
            ctx.errorParams('没传帖子id！');
            return ;
        }
        const forums = await Forums.findOne({
            where: {
                id: params.forumId,
            },
            raw: true,
        });
        if (!forums) {
            ctx.errorParams('帖子id有问题哟！');
            return ;
        }

        const comment = await ForumsComment.create({
            ...params,
            userName: ctx.session.sessionData ? ctx.session.sessionData.nickName : '',
        });
        forums.commentNumber += 1;

        await Forums.update({
            ...forums,
        }, {
            where: {
                id: forums.id
            },
        });

        ctx.success('评论成功！', comment);
    },
    async getComment(ctx) {
        const params = ctx.query;
        // const count = await Words.count();
        const {id} = params;
        if (!id) {
            ctx.errorParams('请传帖子id！');
            return ;
        }
        const info = await ForumsComment.findAll({
            where: {
                forumId: id,
            },
            raw: true,
        });

        ctx.success('查询成功！', info);
    },
    async addForum(ctx) {
        const params = ctx.request.body;
        if (!params.title) {
            ctx.errorParams('请输入帖子标题！');
            return ;
        } else if (!params.content) {
            ctx.errorParams('请输入帖子内容！');
            return ;
        } else if ('number' !== typeof params.type) {
            ctx.errorParams('请选择帖子类型！');
            return ;
        }
        const info = await Forums.create({
            ...params,
            commentNumber: 0,
            userName: ctx.session.sessionData ? ctx.session.sessionData.nickName : '',
        }, {
            raw: true
        });

        ctx.success('查询成功！', info);
    },
    async deleteForum(ctx) {
        const ids = ctx.request.body.ids;
        if (!ids) {
            ctx.errorParams('请传删除id！');
            return ;
        }
        await Forums.destroy({
            where: {
                id: {
                    [Op.in]: ids,
                }
            }
        });

        ctx.success('删除成功！', {
            id: ids,
        })
    }
};


module.exports = {
    ForumsModel,
    Forums
};